package org.akomantoso.core.data;

import java.io.IOException;
import java.util.Calendar;

import org.akomantoso.core.data.attribute.Date;
import org.akomantoso.core.data.attribute.Source;
import org.akomantoso.core.data.type.EventType;
import org.akomantoso.core.data.type.YesNoType;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.DateUtil;

/**
 * The element eventInfo is a metadata element specifying facts about an event that had an effect on the document. For
 * each event, a date, a type and a document that generated the event must be referenced.
 * 
 * <pre>
 * <xsd:element name="eventRef">
 * 		<xsd:complexType>
 * 			<xsd:complexContent>
 * 				<xsd:extension base="anyOtherType">
 * 					<xsd:attributeGroup ref="date"/>
 * 					<xsd:attributeGroup ref="source"/>
 * 					<xsd:attribute name="type" type="eventType"/>
 * 					<xsd:attribute name="originatingExpression" type="yesNoType"/>
 *				</xsd:extension>
 *			</xsd:complexContent>
 * 		</xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class EventRef extends AnyOtherType implements Date , Source{

	public static final String ELEMENT = "eventRef";
	
	public static final String ATTRIBUTE_TYPE = "type";
	public static final String ATTRIBUTE_ORIGINATION_EXPRESSION = "originatingExpression";
	
	private String _source;
	private Calendar _calendar;
	private EventType _eventType;
	private YesNoType _yesNoType;
	
	/**
	 * @see org.akomantoso.core.data.attribute.Source#getSource()
	 */
	public String getSource() {
		return _source;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Source#setSource(java.lang.String)
	 */
	public void setSource(String source) {
		_source = source;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Date#getDate()
	 */
	public Calendar getDate() {
		return _calendar;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Date#setDate(java.util.Calendar)
	 */
	public void setDate(Calendar date) {
		_calendar = date; 
	}

	/**
	 * @return the eventType
	 */
	public EventType getEventType() {
		return _eventType;
	}

	/**
	 * @param eventType the eventType to set
	 */
	public void setEventType(EventType eventType) {
		_eventType = eventType;
	}

	/**
	 * @return the yesNoType
	 */
	public YesNoType getYesNoType() {
		return _yesNoType;
	}

	/**
	 * @param yesNoType the yesNoType to set
	 */
	public void setYesNoType(YesNoType yesNoType) {
		_yesNoType = yesNoType;
	}

	
	/**
	 * @see org.akomantoso.core.data.AnyOtherType#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeDate(out,this);
		OutputUtil.writeSource(out, this);
		OutputUtil.writeAttribute(out, ATTRIBUTE_TYPE, _eventType);
		OutputUtil.writeAttribute(out, ATTRIBUTE_ORIGINATION_EXPRESSION, _yesNoType);
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.AnyOtherType#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		_calendar = DateUtil.convert(in.readAttribute(Date.ATTRIBUTE));
		_source = in.readAttribute(Source.ATTRIBUTE);
		_eventType = EventType.checkValueOf(in.readAttribute(ATTRIBUTE_TYPE));
		_yesNoType = YesNoType.checkValueOf(in.readAttribute(ATTRIBUTE_ORIGINATION_EXPRESSION));
		super.readExternal(in);
	}
	
}